﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
//using Microsoft.VisualBasic.FileIO;
using SmartMotion.Core;
using SmartMotion.Properties;
using WeifenLuo.WinFormsUI.Docking;
using UIOption = Microsoft.VisualBasic.FileIO.UIOption;

namespace SmartMotion.AllPages.DockPanels
{
    public partial class FrmToolBox : DockContent
    {
        private ToolMgr _toolMgr;
        public FrmToolBox()
        {
            InitializeComponent();

        }

        private void FrmToolBox_Load(object sender, EventArgs e)
        {
            _toolMgr=ToolMgr.Ins;
            HideOnClose = true;
            ShowTolls();

        }

        private void ShowTolls()
        {
            ToolMgr.Ins.LoadTools();
            tv.Nodes.Clear();
            imageList1.Images.Clear();
            imageList1.Images.Add(Resources.@ToolBox);
            foreach (var image in _toolMgr.Images)
            {
                imageList1.Images.Add(image.Key,image.Value);
            }

            var groups = _toolMgr.Tools.Select(tool => tool.Group).ToList().Distinct().ToList();

            foreach (var groupName in groups)
            {
                TreeNode gpNode = new TreeNode()
                {
                    Text = string.IsNullOrEmpty(groupName) ? "Default" : groupName
                };

                foreach (var tool in _toolMgr.Tools)
                {
                    if (tool.Group==groupName)
                    {
                        TreeNode toolNode = new TreeNode(tool.ItemName);
                        int index = -1;
                        for (int i = 0; i < imageList1.Images.Keys.Count; i++)
                        {
                            var img = imageList1.Images.Keys[i];
                            if (img==tool.Icon)
                            {
                                index = i;
                                break;
                            }
                        }

                        toolNode.ImageIndex = index;
                        toolNode.SelectedImageIndex = index;
                        gpNode.Nodes.Add(toolNode);
                    }
                }

                tv.Nodes.Add(gpNode);
            }   

            tv.ExpandAll();
        }

        private void CreateToolMenu_Click(object sender, EventArgs e)
        {
            FrmCreateTool frmCreateTool = new FrmCreateTool();
            frmCreateTool.ShowDialog();
            ShowTolls();
        }

        private void tv_ItemDrag(object sender, ItemDragEventArgs e)
        {
            DoDragDrop(e.Item, DragDropEffects.Copy);
        }

        private void 删除工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (UIMessageBox.ShowAsk($"是否将工具[{tv.SelectedNode.Text}]放入回收站?"))
            {
                string filePath = $"{_toolMgr.ToolDirPath}{tv.SelectedNode.Text}.tool";
                FileSystem.DeleteFile(filePath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
                tv.SelectedNode.Remove();
                _toolMgr.LoadTools();
            }
        }
    }
}
